<?php
global $Config;
require_once($Config->WebCMSRoot.'Base/Model.php');
require_once($Config->WebCMSRoot.'Data/Slideshow.php');

Class SlideshowModel extends BaseModel
{
	var $Key;
	var $Description;
	var $NavigationID;
	
	var $Images;
	
	function SlideshowModel($array = null)
	{
		$this->Key = '';
		$this->Description = '';
		$this->NavigationID = -1;
		
		$this->Images = array();
		
		if (isset($array))
		{
			$this->Key         = stripslashes($array['Key']);
			$this->Description = stripslashes($array['Description']);
			$this->NavigationID      = stripslashes($array['NavigationID']);

			parent::BaseModel($array['ID']);
		}
		else
		{
			parent::BaseModel();
		}
	}
	
	function Retrieve(&$parameters)
	{
		$db = new SlideshowData();
		$this->Parameters = &$parameters;
		
		$this->Key       = $this->GetParameter('key');
		$this->NavigationID    = $this->GetParameter('navigationID');

		$db->Retrieve(&$this);
	}
	
	function RetrieveNavigationLinks(&$parameters)
	{
		$db = new SlideshowData();
		$this->Parameters = &$parameters;
		
		$this->ID   	 = $this->GetParameter('id');

		return $db->RetrieveNavigationLinks(&$this);
	}
	
	function Delete()
	{
		$db = new SlideshowData();
		$db->Delete(&$this);
	}

	function RetrieveImages()
	{
		$db = new SlideshowData();
		$imageIDs = $db->RetrieveImages($this);

		foreach ($imageIDs as $key=>$val)
		{
			$image = new ImageModel();
			$image->ID = $val;
	  
	  		$imageDb = new ImageData();
	  		$imageDb->Retrieve(&$image);

			if (count($image->Collection) > 0)
			{
				$image->Collection[0]->_parentID = $this->ID;
				$this->Images[] = $image->Collection[0];
			}
		}
	}
	
	function DeleteImages()
	{
		$db = new SlideshowData();
		$db->DeleteImages(&$this);
	}
	
	function AddImage($image)
	{
		$db = new SlideshowData();
		$db->AddImage(&$this->Collection[0], &$image);
		$this->Images[] = $image;
	}

	function Persist($navigation = null)
	{
		$db = new SlideshowData();

		$db->Update(&$this);
		
		if ($navigation != null)
		{
			// Delete previous links
			$db->DeleteNavigation($this);

			// Insert new values
			$db->AddNavigation(&$this, $navigation);
		}
	}
	
	function FillFromArray($array)
	{
		
	}
}
?>